package com.oocl.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.constant.ApplyStatus;
import com.oocl.pojo.Merchant;
import com.oocl.service.MerchantManager;
import com.oocl.service.factory.MerchantManagerFactory;

@WebServlet("/sec/ban")
public class BanMerchantServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantManager merchantManager;
     
	private Logger logger = Logger.getLogger(BanMerchantServlet.class);
    public BanMerchantServlet() {
        super();
        merchantManager = MerchantManagerFactory.getInstance();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String ban = request.getParameter("change");
    	String mid = request.getParameter("mid");
    	int m=0;
    	if((ban!=null) && (ban.equals("black"))){
    		m=merchantManager.updateIsBan(mid, true);
    	}
    	if((ban!=null) && (ban.equals("white"))){
    		m=merchantManager.updateIsBan(mid, false);
    	}
    	List<Merchant> merchants = merchantManager.findMerchantByStatus(ApplyStatus.APPROVED);
		if(!merchants.isEmpty() && m>0){
			request.setAttribute("merchant", merchants);
			request.getRequestDispatcher("../WEB-INF/views/pass.jsp").forward(request, response);
			logger.info("ban merchant");
		}else{
			response.getWriter().write("error");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
